#include "comm.h"
using namespace std;

// 读取
int main()
{
    // extern int errno;
    umask(0);
    if (mkfifo(IPC_PATH, 0600) != 0)
    {
        cerr << "mkfifo error" << endl;
        return 1;
    }
    // cout<<"hello,server"<<endl;
    int pipeFd = open(IPC_PATH, O_RDONLY);
    if (pipeFd < 0)
    {
        cerr << "Open fifo error" << endl;
        return 2;
    }

// 正常的通信过程
#define NUM 1024
    char buffer[NUM];
    while (true)
    {
        ssize_t s = read(pipeFd, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            buffer[s] = '\0';
            cout << "客户端——>服务器#" << buffer << endl;
        }
        else if (s == 0)
        {
            cout << "客户退出，服务器也将退出！";
            break;
        }
        else
        {
            cout << "read:" << strerror(errno) << endl;
        }
    }
    close(pipeFd);
    cout << "服务端退出！" << endl;
    unlink(IPC_PATH); // 删除创建的管道文件 .fifo
    return 0;
}